package com.theme.passport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.common.mvc.view.R;
import com.theme.passport.dao.SysMessageDao;
import com.theme.passport.entity.SysMessageEntity;
import com.theme.passport.entity.SysMessageUserEntity;
import com.theme.passport.entity.SysUserEntity;
import com.theme.passport.entity.UserEntity;
import com.theme.passport.entity.constant.MessageUserTypeEnum;
import com.theme.passport.service.SysMessageService;
import com.theme.passport.service.SysMessageUserService;
import com.theme.passport.service.SysUserService;
import com.theme.passport.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("sysMessageService")
public class SysMessageServiceImpl extends ServiceImpl<SysMessageDao, SysMessageEntity> implements SysMessageService {

    @Autowired
    private SysMessageUserService messageUserService;

    @Autowired
    private UserService userService;

    @Autowired
    private SysUserService sysUserService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String keyword = (String) params.get("key");
        String type = (String) params.get("type");
        IPage<SysMessageEntity> page = this.page(
                new Query<SysMessageEntity>().getPage(params),
                new QueryWrapper<SysMessageEntity>()
                        .eq(StringUtils.isNotBlank(keyword), "type", type)
                        .like(StringUtils.isNotBlank(keyword), "title", keyword)
                        .or().like(StringUtils.isNotBlank(keyword), "content", keyword)
        );
        return new PageUtils(page);
    }

    @Override
    public R sendMessage(SysMessageEntity messageEntity) {
        int insert = this.baseMapper.insert(messageEntity);
        if (messageEntity.getGroupType() == 1) {
            List<SysUserEntity> listUser = sysUserService.list();
            if (listUser != null && listUser.size() > 0) {
                listUser.forEach(userEntity -> {
                    SysMessageUserEntity messageUser = new SysMessageUserEntity();
                    messageUser.setMessageId(Long.valueOf(insert));
                    messageUser.setMessageType(messageEntity.getType());
                    messageUser.setToType(MessageUserTypeEnum.PORTAL.getValue());
                    messageUser.setToUserId(userEntity.getUserId());
                    messageUser.setToNickName(userEntity.getNickname());
                    messageUser.setCreateTime(new Date());
                    messageUser.setStatus(1);
                    messageUserService.save(messageUser);
                });
            }
            return R.ok();
        }
        if (messageEntity.getGroupType() == 2) {
            List<UserEntity> listUser = userService.list();
            if (listUser != null && listUser.size() > 0) {
                listUser.forEach(userEntity -> {
                    SysMessageUserEntity messageUser = new SysMessageUserEntity();
                    messageUser.setMessageId(Long.valueOf(insert));
                    messageUser.setMessageType(messageEntity.getType());
                    messageUser.setToType(MessageUserTypeEnum.PORTAL.getValue());
                    messageUser.setToUserId(userEntity.getId());
                    messageUser.setToNickName(userEntity.getNickname());
                    messageUser.setCreateTime(new Date());
                    messageUser.setStatus(1);
                    messageUserService.save(messageUser);
                });
            }
            return R.ok();
        }
        return R.ok();
    }

    @Override
    public PageUtils querySysNotice(int groupType, int type) {
        Map<String, Object> params = new HashMap<>();
        params.put("group_type", groupType);
        params.put("type", type);
        params.put("page", 1 + "");
        params.put("limit", 2 + "");
        IPage<SysMessageEntity> page = this.page(
                new Query<SysMessageEntity>().getPage(params),
                new QueryWrapper<SysMessageEntity>()
                        .eq("group_type", groupType)
                        .eq("type", type)
                        .orderByDesc("create_time")
        );
        return new PageUtils(page);
    }

}